package map;

import com.sun.deploy.util.StringUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

public class MostCommonWord819 {
    public String mostCommonWord(String paragraph, String[] banned) {
        HashMap<String, Integer> map = new HashMap<>();
        Set<String> set = new HashSet<>();
        int max = 0;
        String result = "";
        for (int i = 0; i < banned.length; i++) {
            set.add(banned[i]);
        }

        int begin = 0;
        int index = 1;
        while (index <= paragraph.length()) {
            if (index==paragraph.length()||!Character.isLetter(paragraph.charAt(index))) {
                String word = paragraph.substring(begin, index).trim().toLowerCase();
                int value = map.getOrDefault(word, 0) + 1;
                map.put(word, value);
                if (value > max && !set.contains(word)) {
                    result = word;
                    max = value;
                }
                index++;
                begin = index;
                index++;
            } else {
                index++;
            }
        }
        return result;
    }
}
